package com.jic.member.impl.power;
import	java.util.ArrayList;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.jic.member.entity.MememberLegalLogisticsIncrement;
import com.jic.member.entity.MememberLegalRuleLogistics;
import com.jic.member.entity.power.*;
import com.jic.member.enums.POWER_NO;
import com.jic.member.mapper.MememberGradeMapper;
import com.jic.member.mapper.MememberLegalLogisticsIncrementMapper;
import com.jic.member.mapper.MememberLegalRuleActionMapper;
import com.jic.member.mapper.MememberLegalRuleLogisticsMapper;
import com.jic.member.mapper.power.*;
import com.jic.member.request.power.LegalLogisticsIncrement;
import com.jic.member.request.power.MememberLegalRuleRequest;
import com.jic.member.response.power.MememberLegalRuleResponse;
import com.jic.member.response.power.SpuInfo;
import com.jic.member.service.power.MemberRuleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;


/**
 * 等级权益
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-04-04 10:45:04
 */
@Slf4j
@Service
public class MemberRuleServiceImpl implements MemberRuleService {

    @Resource
    MegradeRuleMapper megradeRuleMapper;
    @Resource
    MememberGradeMapper mememberGradeMapper;
    @Resource
    MememberLegalRuleMapper mememberLegalRuleMapper;
    @Resource
    MememberLegalRuleActionMapper mememberLegalRuleActionMapper;
    @Resource
    private MememberLegalLogisticsIncrementMapper mememberLegalLogistics;//递增表
    @Resource
    private MememberLegalRuleLogisticsMapper mememberLegalRuleLogisticsMapper;//物流主表
    @Resource
    private MepowerStoreMapper mepowerStoreMapper;
    @Resource
    private MepowerProductMapper mepowerProductMapper;
    @Resource
    private MeproductMapper mepowerProductMap;

    /*
     * 根据会员等级和组织id 展示当前用户的权益*/
    @Override
    public List<MememberLegalRuleResponse> queryMemberRule(MememberLegalRuleRequest mememberLegalRuleRequest) {
        log.info("开始查询会员权益列表==》总入参,{}",mememberLegalRuleRequest);
        List<MememberLegalRuleResponse> mememberLegalRuleResponse = new ArrayList<>();
        //根据会员等级和组织id查询 mem_grade_rule
        MegradeRule meberRule = new MegradeRule() {{
            setDeptId(mememberLegalRuleRequest.getDeptId());
            setGradeId(mememberLegalRuleRequest.getGradeId());
            setDeleteFlag(0);
            setStoreMember(2L);
        }};
        log.info("查询会员权益列表==》查询会员等级权益关联表==》入参,{}", JSON.toJSONString(meberRule));
        List<MegradeRule> megradeRuleList = megradeRuleMapper.query(meberRule);
        log.info("查询会员权益列表==》查询会员等级权益关联表==》结果,{}",megradeRuleList);
        //获取权益
        megradeRuleList.forEach(items -> {
            MememberLegalRuleResponse memberLegalRuleResponse = new MememberLegalRuleResponse();
            MememberLegalRule mememberLegalRule = mememberLegalRuleMapper.queryByPrimaryKey(items.getLegalRuleId());
            memberLegalRuleResponse.setLegalRuleName(mememberLegalRule.getLegalRuleName());
            memberLegalRuleResponse.setLegalRuleType(mememberLegalRule.getLegalRuleType());
            if (POWER_NO.SHOPPING_NO.getName().equals(mememberLegalRule.getLegalRuleType()) || POWER_NO.INTEGRAL_NO.getName().equals(mememberLegalRule.getLegalRuleType())
                    || POWER_NO.ACTIVITY_NO.getName().equals(mememberLegalRule.getLegalRuleType())
                    || POWER_NO.CONTENTL_NO.getName().equals(mememberLegalRule.getLegalRuleType())
                    || POWER_NO.SERVICE_NO.getName().equals(mememberLegalRule.getLegalRuleType())
                    || POWER_NO.GRADE_NO.getName().equals(mememberLegalRule.getLegalRuleType())) {
                log.info("查询会员权益列表==》【权益类型】不为空,{}",mememberLegalRule.getLegalRuleType());
                //根据legal_rule_id 和 legal_rule_type查询mem_member_legal_rule_action
                Integer legalRuleType = Integer.parseInt(mememberLegalRule.getLegalRuleType());
                log.info("查询会员权益列表==》查询会员等级管理-权益规则-购物-积分-活动-服务-内容-等级升级表==》入参，{},{}",mememberLegalRule.getId(),legalRuleType);
                memberLegalRuleResponse = mememberLegalRuleActionMapper.queryInfoByLegalRuleType(mememberLegalRule.getId(), legalRuleType);
                log.info("查询会员权益列表==》查询会员等级管理-权益规则-购物-积分-活动-服务-内容-等级升级表==》结果，{}",memberLegalRuleResponse);
            } else {
                log.info("查询会员权益列表==》查询会员等级管理-权益规则-物流权益表==》入参：会员等级权益规则主表ID,{}",mememberLegalRule.getId());
                MememberLegalRuleLogistics ruleLogistics = mememberLegalRuleLogisticsMapper.queryInfoByLegalRuleId(mememberLegalRule.getId());
                log.info("查询会员权益列表==》查询会员等级管理-权益规则-物流权益表==》结果,{}",ruleLogistics);
                if (null != ruleLogistics) {
                    if (ruleLogistics.getFreightMode() == 1) {
                        memberLegalRuleResponse.setFreightMode(1);
                    } else {
                        memberLegalRuleResponse.setFreightMode(2);
                        MememberLegalLogisticsIncrement increment = new MememberLegalLogisticsIncrement() {{
                            setRuleLogisticsId(ruleLogistics.getId());
                            setDeleteFlag(0);
                        }};
                        log.info("查询会员权益列表==》查询会员等级管理-权益规则-物流权益递增表==》入参,{}",increment);
                        List<MememberLegalLogisticsIncrement> incrementList = mememberLegalLogistics.query(increment);
                        log.info("查询会员权益列表==》查询会员等级管理-权益规则-物流权益递增表==》结果,{}",incrementList);
                        List<LegalLogisticsIncrement> logisticsIncrement = new ArrayList<>();
                        for (int i = 0; i < incrementList.size(); i++) {
                            LegalLogisticsIncrement logistics = new LegalLogisticsIncrement();
                            logistics.setId(incrementList.get(i).getId());
                            logistics.setSatisfyOrder(incrementList.get(i).getSatisfyOrder());
                            logistics.setReductionMoney(incrementList.get(i).getReductionMoney());
                            logisticsIncrement.add(logistics);
                        }
                        memberLegalRuleResponse.setLogisticsIncrement(logisticsIncrement);
                        log.info("查询会员权益列表==》满减满单位元运费==》结果,{}",memberLegalRuleResponse);
                    }
                }

            }

            //查询权益店铺信息
            MepowerStore mepowerStore = new MepowerStore();
            mepowerStore.setLegalRuleId(items.getLegalRuleId());
            mepowerStore.setDeleteFlag(0);
            log.info("查询会员权益列表==》查询权益店铺信息==》入参,{}",mepowerStore);
            List<MepowerStore> storeList = mepowerStoreMapper.query(mepowerStore);
            List<Long> store = new ArrayList<>();
            storeList.forEach(storeInfo -> {
                store.add(storeInfo.getStoreId());
            });
            log.info("查询会员权益列表==》查询权益店铺信息==》结果,{}",store);
            memberLegalRuleResponse.setStoreIds(store);
            //查询权益商品信息
            MepowerProduct mepowerProduct = new MepowerProduct();
            mepowerProduct.setLegalRuleId(items.getLegalRuleId());
            mepowerProduct.setDeleteFlag(0);
            log.info("查询会员权益列表==》查询权益商品信息==》入参,{}",mepowerProduct);
            List<MepowerProduct> powerProduct = mepowerProductMapper.query(mepowerProduct);
            log.info("查询会员权益列表==》查询权益商品信息==》结果,{}",powerProduct);
            Meproduct meproduct = new Meproduct();
            if (CollectionUtils.isNotEmpty(powerProduct)) {
                meproduct.setPowerProductId(powerProduct.get(0).getId().longValue());
                meproduct.setMpParentId(0L);
                meproduct.setDeleteFlag(0);
                log.info("查询会员权益列表==》查询权益商品关联表==》入参,{}",meproduct);
                List<Meproduct> meproductList = mepowerProductMap.query(meproduct);
                log.info("查询会员权益列表==》查询权益商品关联表==》结果,{}",meproductList);
                Meproduct spu = new Meproduct();
                List<SpuInfo> productIds = new ArrayList<>();

                if (CollectionUtils.isNotEmpty(meproductList)) {
                    meproductList.forEach(product -> {
                        SpuInfo spuInfo = new SpuInfo();
                        if (product.getMpParentId() == 0) {
                            spuInfo.setSpuId(product.getMpId());
                            spu.setMpParentId(product.getMpId());
                            spu.setPowerProductId(powerProduct.get(0).getId().longValue());
                            spu.setDeleteFlag(0);
                            List<Meproduct> resultProduct = mepowerProductMap.query(spu);
                            List<Long>skuIds = new ArrayList<> ();
                            resultProduct.forEach(sku->{
                                skuIds.add(sku.getMpId());
                            });
                            spuInfo.setSkuIds(skuIds);
                        }
                        productIds.add(spuInfo);
                        log.info("查询会员权益列表==》商品集合ids,{}",productIds);
                    });
                }
                memberLegalRuleResponse.setProductIds(productIds);
            }
            mememberLegalRuleResponse.add(memberLegalRuleResponse);
        });
        log.info("结束查询会员权益列表==》返回值,{}",mememberLegalRuleResponse);
        return mememberLegalRuleResponse;
    }
}
